/****************************************************************************
*                                                                           *
* dxMeshAnimation.h -- Mesh Animation                                       *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2009).                  *
*                                                                           *
****************************************************************************/

#pragma once

#include <d3d9.h>
#include <d3dx9.h>
#include <string>

#include ".\dxMeshStructures.h"

struct D3DVERTEX
{
    D3DXVECTOR3 p;
    D3DXVECTOR3 n;
    FLOAT tu, tv;

    static const DWORD FVF;
};

class dxMeshAnimation
{
	private:
		LPDIRECT3DDEVICE9 m_d3dDevice;

		LPD3DXFRAME                 m_frameRoot;
		LPD3DXFRAME                 m_frameFind;
		LPD3DXANIMATIONCONTROLLER   m_animController;
		D3DXMESHCONTAINER_EXTENDED* m_firstMesh;

		D3DXMATRIX *m_boneMatrices;
		int m_maxBones;
		
		unsigned int m_currentAnimationSet;	
		unsigned int m_numAnimationSets;
		unsigned int m_currentTrack;
		float m_currentTime;
		float m_speedAdjust;

		D3DXVECTOR3 m_sphereCentre;
		float m_sphereRadius;

		std::string m_filename;

		void UpdateFrameMatrices(const D3DXFRAME *frameBase, const D3DXMATRIX *parentMatrix);
		void DrawFrame(LPD3DXFRAME frame) const;
		void DrawMeshContainer(LPD3DXMESHCONTAINER meshContainerBase, LPD3DXFRAME frameBase) const;
		void SetupBoneMatrices(D3DXFRAME_EXTENDED *pFrame, LPD3DXMATRIX pParentMatrix);	
		
	public:
		dxMeshAnimation(LPDIRECT3DDEVICE9 d3dDevice);
		~dxMeshAnimation(void);

		bool Load(const std::string &filename);
		void FrameMove(float elapsedTime,const D3DXMATRIX *matWorld);
		
		void Render() const;
		void SetAnimationSet(unsigned int index);

		void NextAnimation();
		void AnimateFaster();
		void AnimateSlower();
		
		void processMeshData();

		D3DXVECTOR3 GetInitialCameraPosition() const;
		unsigned int GetCurrentAnimationSet() const {return m_currentAnimationSet;}
		std::string GetAnimationSetName(unsigned int index);
		std::string GetFilename() const {return m_filename;}
};